import 'package:flutter/material.dart';
import 'package:silver_flutter/chapter_page.dart';
import 'package:silver_flutter/model/book.dart';
import 'package:silver_flutter/model/chapter.dart';
import 'package:silver_flutter/api.dart' as api;

class DemoPage extends StatefulWidget {
  final Book book;

  DemoPage({Key key, this.book}) : super(key: key);

  @override
  State<StatefulWidget> createState() {
    return new _DemoPageState();
  }
}

class _DemoPageState extends State<DemoPage> {
  final _tabs = ['详情', '目录'];

  @override
  Widget build(BuildContext context) {
    return new DefaultTabController(
      length: _tabs.length,
      child: new Scaffold(
        body: new NestedScrollView(
          headerSliverBuilder: (BuildContext context, bool innerBoxIsScrolled) {
            return <Widget>[
              new SliverAppBar(
                title: new Text(widget.book.title),
                flexibleSpace: new FlexibleSpaceBar(
                  background: new Container(
                    decoration: new BoxDecoration(
                      color: const Color(0xff000000),
                      image: new DecorationImage(
                        fit: BoxFit.cover,
                        colorFilter: new ColorFilter.mode(Colors.black.withOpacity(0.5), BlendMode.dstATop),
                        image: new NetworkImage(
                          widget.book.coverImageUrl,
                        ),
                      ),
                    ),
                  ),
                ),
                pinned: true,
                floating: true,
                expandedHeight: 200.0,
                forceElevated: innerBoxIsScrolled,
                bottom: new TabBar(
                  tabs:
                      _tabs.map((String name) => new Tab(text: name)).toList(),
                ),
              ),
            ];
          },
          body: new TabBarView(
            children: [
              new Padding(
                padding: const EdgeInsets.all(10.0),
                child: new Text(widget.book.description),
              ),
              new FutureBuilder<List<Chapter>>(
                future: api.fetchChapters(widget.book.id),
                builder: (context, snapshot) {
                  if (snapshot.hasData) {
                    return new GridView.count(
                        primary: false,
                        padding: const EdgeInsets.all(10.0),
                        crossAxisSpacing: 10.0,
                        crossAxisCount: 5,
                        mainAxisSpacing: 10.0,
                        childAspectRatio: 2.0 / 1.0,
                        children: snapshot.data
                            .map(
                              (chapter) => new RaisedButton(
                            onPressed: () {
                              Navigator.push(
                                context,
                                new MaterialPageRoute(
                                    builder: (context) => new ChapterPage(
                                      bookId: widget.book.id,
                                      chapter: chapter,
                                    )),
                              );
                            },
                            child: new Text(chapter.number.toString()),
                          ),
                        )
                            .toList());
                  } else if (snapshot.hasError) {
                    return new Text("${snapshot.error}");
                  }

                  // By default, show a loading spinner
                  return new Center(child: new CircularProgressIndicator());
                },
              ),
            ]
          ),
        ),
      ),
    );
  }
}
